<?php
$item = $model;
$path = Yii::app()->baseUrl."/upload_files/products";

$folder_dir = Yii::getPathOfAlias('webroot')."/wwwroot/{$path}/{$item->image}";

if (!is_file($folder_dir)) {
	$src = Yii::app()->baseUrl."/uploads/no-image.png";
} else {
	$src = Yii::app()->baseUrl."/wwwroot/{$path}/{$item->image}";
}

?>
<div class="images" style=" width: 270px; float: left; ">
		<a itemprop="image" href="<?php echo $src;?>" class="zoom" rel="thumbnails" title="<?php echo $item->name;?>">
		
			<img width="209" height="350" src="<?php echo $src;?>" alt="<?php echo $item->name;?>" style="height: auto;padding: 9px;border: 1px solid #e6e6e6;margin-bottom: 12px;"/>
		</a>
		<div class="thumbnails">
			<?php
			if(!empty($model->images)){
				foreach($model->images as $key=>$value){
				
					$path = Yii::app()->baseUrl."/upload_files/product_images/thumbs";
					$link = Yii::app()->baseUrl."/upload_files/product_images";

					$folder_dir = Yii::getPathOfAlias('webroot')."/wwwroot/{$path}/{$value->image}";

					if (!is_file($folder_dir)) {
						$imageLink = $src = Yii::app()->baseUrl."/uploads/no-image.png";
					} else {
						$src = Yii::app()->baseUrl."/wwwroot/{$path}/{$value->image}";
						$imageLink = Yii::app()->baseUrl."/wwwroot/{$link}/{$value->image}";
					}
			?>
				<a href="<?php echo $imageLink; ?>" title="&#039;Coral Reef&#039; (dress)" rel="thumbnails" class="zoom first">
					<img width="119" height="200" src="<?php echo $src;?>" class="attachment-shop_thumbnail"  alt="<?php echo $model->name;?>" />
				</a>
			<?php
				}
			}
			?>
			
		</div>
	</div>				
	<div class="summary" style=" width: 270px; float: right; ">
		<h1 itemprop="name" class="product_title page-title"><?php echo isset($model->name) ? $model->name : '';?></h1>
		<p itemprop="price" class="price"><span class="from">From: </span><span class="amount">&#36;<?php echo isset($model->price) ? $model->price : 0?></span></p>
		<div itemprop="description">
			<p>SOLD OUT</p>
			<?php echo isset($model->description) ? $model->description : ''?>
		</div>
		<script type="text/javascript">
			var product_variations = [{"variation_id":162,"attributes":{"attribute_pa_style":"dress"},"image_src":"","image_link":"","price_html":"<span class=\"price\"><span class=\"amount\">&#36;310<\/span><\/span>","availability_html":"<p class=\"stock out-of-stock\">Out of stock<\/p>","sku":"SKU: ","min_qty":1,"max_qty":"0","is_downloadable":false,"is_virtual":false},{"variation_id":163,"attributes":{"attribute_pa_style":"long-kaftan"},"image_src":"","image_link":"","price_html":"<span class=\"price\"><span class=\"amount\">&#36;280<\/span><\/span>","availability_html":"<p class=\"stock out-of-stock\">Out of stock<\/p>","sku":"SKU: ","min_qty":1,"max_qty":"0","is_downloadable":false,"is_virtual":false}];
		</script>
		<form class="variations_form cart" target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
			<input type="hidden" name="business" value="<?php echo Yii::app()->params['mailpp'];?>">
			<input type="hidden" name="cmd" value="_cart">
			<input type="hidden" name="add" value="1">
			<input type="hidden" name="on0" value="Option">
            <input type="hidden" name="item_name" value="<?php echo $item->name;?>">
			<input type="hidden" name="amount" value="<?php echo $model->price;?>" id="amount">
			<input type="hidden" name="currency_code" value="USD">
			<input type="hidden" name="shopping_url" value="<?php echo Yii::app()->request->requestUri;?>">
			<table class="variations" cellspacing="0">
				<?php
				$style = explode(";",$model->style);
				$dataStyle = array();
				if(!empty($style)){
					foreach($style as $k=>$v){
						if(trim($v) != "") $dataStyle[] = $v;
					}
				}
				?>
				<tbody>
					<tr>
						<td><label for="pa_style">Style</label></td>
						<td>
							<select id="pa_style" name="os0">
								<option value="0">Choose an option&hellip;</option>
								<?php
								foreach($dataStyle as $value){
								?>
									<option value="<?php echo trim($value);?>" ><?php echo $value;?></option>
								<?php
								}
								
								?>
								
							</select> 
							
							<a class="reset_variations" style="display:none" onclick="resetSelection()" href="#reset">Reset selection</a>
						</td>
					</tr>
				</tbody>
			</table>
			<div class="single_variation_wrap" style="display:none;">
				
				<div class="single_variation"><span class="price"><span class="amount">$<label id="aPrice"><?php echo $model->price;?></label></span></span>
					<?php
					if($model->quantity == 0){
					?><p class="stock out-of-stock">Out of stock</p>
					<?php
					}
					?>
					</div>
				
				<div class="variations_button">
					<input type="hidden" name="variation_id" value="" />
						<div class="quantity">
							<input type="button" value="-" id="minus1" class="minus qtyminus" style="margin-right: -4px;"  field='quantity'>
							<input name="quantity" data-min="0" data-max="<?php echo $model->quantity;?>" value="1" size="4" name="quantity" title="Qty" id="quantity" class="input-text qty text" maxlength="12" />
							
							<input type="button" value="+" id="add1" class="plus qtyplus" style="margin-left: -4px;" field='quantity'>
						</div>
					<button type="button" id="buttonAddToCart" class="button alt">Add to cart</button>
					
				</div>
				
			</div>
			<div><input type="hidden" name="product_id" value="<?php echo $model->id;?>" /></div>
		</form>
		
		<script>
		$().ready(function(e){
			$("#buttonAddToCart").live('click',function(e){
				
				$.ajax({
				type:"POST",
				url:"<?php echo Yii::app()->request->requestUri;?>",
				data:$('.variations_form').serialize(),
				success:function(res){
					// console.log(res);
					// return false;
					if(res.error){
						$("#woocommerce_error").html('<p>'+res.message+'</p>');
						$("#woocommerce_error").show();
						// alert(res.message);
					}else{
						window.location.href="<?php echo Yii::app()->createUrl('/products/viewCart');?>"
						
					}
				
				},error: function (xhr, ajaxOptions, thrownError) {
					console.log(xhr.responseText);
				}
			});
			});
		});

		function resetSelection(){
			$('.reset_variations').hide('slow');  
			$('.single_variation_wrap').hide('slow');  
			$('#quantity').val(0);  
			$('#pa_style option[value="0"]').attr("selected", "selected");
			$('#pa_style').parent().find('span').html( $('#pa_style option[value="0"]').text() );
		}
		jQuery(document).ready(function(){
			$("#pa_style").live('change',function(e){
				if($(this).val() != ""){
					$(".reset_variations, .single_variation_wrap").show('slow');
				}
			});
			// This button will increment the value
			$('.qtyplus').click(function(e){
				// Stop acting like a button
				e.preventDefault();
				// Get the field name
				fieldName = $(this).attr('field');
				// Get its current value
				var currentVal = parseInt($('input[name='+fieldName+']').val());
				// If is not undefined
				if (!isNaN(currentVal)) {
					// Increment
					$('input[name='+fieldName+']').val(currentVal + 1);
				} else {
					// Otherwise put a 0 there
					$('input[name='+fieldName+']').val(0);
				}
				$("#aPrice").html($('input[name='+fieldName+']').val() * <?php echo $model->price;?>);
				$("#amount").html($('input[name='+fieldName+']').val() * <?php echo $model->price;?>);
			});
			// This button will decrement the value till 0
			$(".qtyminus").click(function(e) {
				// Stop acting like a button
				e.preventDefault();
				// Get the field name
				fieldName = $(this).attr('field');
				// Get its current value
				var currentVal = parseInt($('input[name='+fieldName+']').val());
				if(currentVal == 1 ) return false;
				// If it isn't undefined or its greater than 0
				if (!isNaN(currentVal) && currentVal > 0) {
					// Decrement one
					$('input[name='+fieldName+']').val(currentVal - 1);
				} else {
					// Otherwise put a 0 there
					$('input[name='+fieldName+']').val(0);
				}
				$("#aPrice").html($('input[name='+fieldName+']').val() * <?php echo $model->price;?>);
				$("#amount").html($('input[name='+fieldName+']').val() * <?php echo $model->price;?>);
			});
		});

		</script>
		<div class="product_meta">
			<span class="posted_in">Category: 
				
				<?php
				if(!empty($model->mapcat)){
					foreach($model->mapcat as $key=>$value){
				?>
					<a href="<?php echo Yii::app()->createUrl('/boutique?category='.$value->cat->alias);?>" rel="tag"><?php echo $value->cat->name;?></a>
					<?php
					if($key + 1 != count($model->mapcat)) echo ", ";
					
					?>
				<?php
					}
				}
				
				?>
				
			</span>
		</div>
	</div>